import logging

import allure
import jsonpath
import pytest

from core.globalContext import Global_Context
from extend.keywords import KeyWords


# @pytest.fixture(scope="session")
# def token_fixture():
#     with allure.step("第一步：登录成功，获取token"):
#         url = "http://shop-xo.hctestedu.com/index.php?s=/api/user/login"
#         params = {"application": "app", "application_client_type": "weixin"}  # 以字典的格式去写
#         data = {"accounts": "hami", "pwd": "123456", "type": "username"}
#         res = KeyWords().request_post(url=url, params=params, data=data)
#         token_res = jsonpath.jsonpath(res.json(), "$..token")[0]
#         return token_res


@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    # 通过 out = yield 定义了一个生成器。在生成器中，res = out.get_result() 获取了测试结果对象。
    out = yield  # 类似于return，但是它返回之后执行完毕会自动回来
    res = out.get_result()
    #  res.when == "call"：表示正在运行调用测试函数的阶段。
    if res.when == "call":
        logging.info(f"用例ID：{res.nodeid}")
        logging.info(f"测试结果：{res.outcome}")
        logging.info(f"故障表示：{res.longrepr}")
        logging.info(f"异常：{call.excinfo}")
        logging.info(f"用例耗时：{res.duration}")
